var PITCHWISE = PITCHWISE || {};
PITCHWISE.multiMap = PITCHWISE.multiMap || {};

PITCHWISE.multiMap = function (options) {
    'use strict';

    //this.authenticated = options.authenticated;
    this.el = {
        'address':$("#address")
    };

    console.log('PITCHWISE.map.options', options);
    this.load();
};
PITCHWISE.multiMap.prototype.events = function () {

};
PITCHWISE.multiMap.prototype.codeAddress = function () {
    var _this = this;
    var infoarray = [];

    $('.addresses').each(function () {
        var element = this;
        var address = $(this).html();
        _this.geocoder.geocode({ 'address':address}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var image = '/wp-content/themes/pitchwise/img/Football-32.png';
                var marker = new google.maps.Marker({
                    map:_this.map,
                    position:results[0].geometry.location,
                    title:$(element).attr('data-title'),
                    animation: google.maps.Animation.DROP,
                    icon: image
                });

                var contentString = '<div>' +
                    '<h3><a href="' + $(element).attr('data-url') + '">' + $(element).attr('data-title') + '</a></h3>'
                    + '<p>' + address + '</p>'
                    + '</div>';

                var infowindow = new google.maps.InfoWindow({
                    content: contentString,
                    maxWidth: 200
                });

                infoarray.push(infowindow);

                google.maps.event.addListener(marker, 'click', function () {
                    for (var a in infoarray) {
                        infoarray[a].close();
                    }
                    infowindow.open(_this.map,marker);
                });
            } else {
                console.log("Geocode was not successful for the following reason: " + status);
            }
        });
    });

};

PITCHWISE.multiMap.prototype.mapInit = function () {
    this.geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(51.526688,-0.119476);
    var mapOptions = {
        zoom:10,
        center:latlng,
        mapTypeId:google.maps.MapTypeId.ROADMAP,
        streetViewControl:false,
        scaleControl:false,
        overviewMapControl:false,
        disableDefaultUI: false,
        panControl: false,
        rotateControl: false,
        mapTypeControl: false,
        zoomControl: false
    }
    this.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
};

PITCHWISE.multiMap.prototype.init = function () {
    var _this = this;

    this.mapInit();
    this.codeAddress();
};

PITCHWISE.multiMap.prototype.load = function () {
    this.events();
    this.init();
    $('html, body').animate({
        scrollTop: $(".span12").offset().top
    }, 2000);
};